package hongvt.quanlynhanvien;

import hongvt.quanlynhanvien.adapter.SalaryReportAdapter;
import hongvt.quanlynhanvien.database.DatabaseManager;
import hongvt.quanlynhanvien.model.Employee;
import hongvt.quanlynhanvien.model.FinanceReport;
import hongvt.quanlynhanvien.model.Timekeeping;
import hongvt.quanlynhanvien.model.WorkingMode;
import hongvt.quanlynhanvien.utils.Define;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.InjectView;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;

public class TabSalaryActivity extends AbstractActivity {

	@InjectView(R.id.listViewTabEmp)
	ListView listView;

	@InjectView(R.id.tv_salary_total)
	TextView salaryTotalTv;

	@InjectView(R.id.tv_bonus_total)
	TextView bonusTotalTv;

	DatabaseManager dbManagement;
	List<Timekeeping> listTimeKeepings;
	List<Employee> listemEmployees;

	List<WorkingMode> listWorkingModes;

	@Override
	protected int getCurrentViewId() {
		// TODO Auto-generated method stub
		return R.layout.activity_tabsalary;
	}

	@Override
	protected void afterInitView() {
		int month = 0;
		int year = 0;

		Bundle extras = getIntent().getExtras();

		if (extras != null) {
			month = Integer.parseInt(extras.getString(Define.MONTH));
			// Log.i("month", month);
			year = Integer.parseInt(extras.getString(Define.YEAR));
		}

		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month);
		int daysQty = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		Log.i("dayYear", String.valueOf(year));
		Log.i("dayMonth", String.valueOf(month));
		Log.i("day", String.valueOf(daysQty));

		dbManagement = new DatabaseManager(context);
		listTimeKeepings = dbManagement.getListTimekeepingByMonth(year, month);

		listemEmployees = dbManagement.getListEmployees();

		List<Employee> tempEmp = new ArrayList<Employee>();
		
		for (Employee emp : listemEmployees) {
			boolean hasExist = false;
			for (Timekeeping tk : listTimeKeepings) {
				if (tk.getEmpId().equals(emp.getEmpId())) {
					hasExist = true;
					break;
				}
			}

			if (!hasExist) {
				tempEmp.add(emp);
			}
		}
		
		
		
		for (Employee emp : tempEmp) {
			Timekeeping tk = new Timekeeping();
			tk.setName(emp.getName());
			listTimeKeepings.add(tk);
		}

		listWorkingModes = dbManagement.getListWorkingMode();

		long salaryTotal = 0;
		long bonusTotal = 0;

		List<FinanceReport> listFinanceReports = new ArrayList<FinanceReport>();
		for (Timekeeping tk : listTimeKeepings) {
			FinanceReport fr = new FinanceReport();
			fr.setEmpName(tk.getName());

			for (WorkingMode wm : listWorkingModes) {
				if (wm.getEmpId().equals(tk.getEmpId())) {
//					double salary = (double) wm.getSalary()
//							* ((double) tk.getQuan() / daysQty);
//					fr.setSalary((long) salary);
					fr.setSalary(wm.getSalary() * tk.getQuan());
					if (tk.getQuan() >= daysQty - 1) {
						fr.setBonus(wm.getBonus());
					}
					break;
				}
			}

			listFinanceReports.add(fr);
			salaryTotal = salaryTotal + fr.getSalary();
			bonusTotal = bonusTotal + fr.getBonus();
		}

		SalaryReportAdapter adapter = new SalaryReportAdapter(context,
				listFinanceReports);
		listView.setAdapter(adapter);

		salaryTotalTv.setText(String.valueOf(salaryTotal));
		bonusTotalTv.setText(String.valueOf(bonusTotal));

	}
}
